import 'package:flutter/material.dart';
import 'package:flutter_compass/flutter_compass.dart';
import 'package:xunlongchi/ui/home/compass/com_center.dart';

import 'package:xunlongchi/widgets/animated_roate.dart';

///指南针
class Compass extends StatelessWidget {
  const Compass({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _buildCompass();
  }

  Widget _buildCompass() {
    return StreamBuilder<double>(
      stream: FlutterCompass.events,
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return const ComCenter();
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return const ComCenter();
        }

        double direction = snapshot.data;

        if (direction == null) {
          return const ComCenter();
        }

        return AnimatedRotation(
          duration: const Duration(milliseconds: 200),
          turn: 1 - ((direction ?? 0) / 360),
          child: const ComCenter(),
        );
      },
    );
  }
}
